package com.sinaapp.gavinzhang.GSpider.http.servlet;

import com.sinaapp.gavinzhang.GSpider.config.Constants;
import com.sinaapp.gavinzhang.GSpider.config.GSpiderConfig;
import com.sinaapp.gavinzhang.GSpider.config.PageProcessorConfig;
import com.sinaapp.gavinzhang.GSpider.http.annotation.ServletClass;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by gavin on 15-8-13.
 */
@ServletClass(requestName = "/status",function = "doGet")
public class StatusServlet extends HttpServlet {

    private static final Logger LOGGER = Logger.getLogger(StatusServlet.class);

    private ExecutorService executor;
    private GSpiderConfig gSpiderConfig;
    private PageProcessorConfig pageProcessorConfig;

    public StatusServlet(ExecutorService executor, GSpiderConfig gSpiderConfig, PageProcessorConfig pageProcessorConfig) {
        this.executor = executor;
        this.gSpiderConfig = gSpiderConfig;
        this.pageProcessorConfig = pageProcessorConfig;
    }

    public StatusServlet() {

    }


    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (executor==null || pageProcessorConfig==null)
        {
            String str = "<html><head><title>status</title></head><body><h1>GSpider</h1><p>Welcome to GSpider</p><p>Welcome to GSpider</p></html>";
            resp.setHeader("Content-Type","text/html;charset=utf-8");
            resp.setHeader("Server","gspider");
            Writer writer = resp.getWriter();
            writer.write(str);
            writer.flush();
            writer.close();
            return;
        }
        int ActiveCount = ((ThreadPoolExecutor)executor).getActiveCount();
        int CorePoolSize = ((ThreadPoolExecutor)executor).getCorePoolSize();
        int PoolSize = ((ThreadPoolExecutor)executor).getPoolSize();
        long CompletedTaskCount = ((ThreadPoolExecutor)executor).getCompletedTaskCount();
        long TaskCount = ((ThreadPoolExecutor)executor).getTaskCount();


        String status = "<html><head><meta charset=\"utf-8\"><title>GSpider status</title></head><body><h1>GSpider status</h1><p>*********************************</p><p>ActiveCount:            "+ActiveCount+"</p><p>CorePoolSize:           "+CorePoolSize+"</p><p>CompletedTaskCount:     "+CompletedTaskCount+"</p><p>TaskCount:              "+TaskCount+"</p><p>foudUrlList:            " + pageProcessorConfig.getFoundUrlList().size()+"</p><p>currentUrlList:         " + pageProcessorConfig.getCurrentUrlList().size()+"</p><p>*********************************</p></body></html>";

        String str = "<html><head><title>"+Constants.STATUS+"</title></head><body>"+status+"</html>";
        resp.setHeader("Content-Type","text/html;charset=utf-8");
        resp.setHeader("Server","gspider");
        Writer writer = resp.getWriter();
        writer.write(str);
        writer.flush();
        writer.close();
    }

    /*public static byte[] readBytes(InputStream in) throws IOException {
        BufferedInputStream bufin = new BufferedInputStream(in);
        int buffSize = Constants.BUFFSIZE;
        ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);
        byte[] temp = new byte[buffSize];
        int size = -1;
        while ((size = bufin.read(temp)) != -1) {
            out.write(temp, 0, size);
            if (size!=1024)break;
        }
        bufin.close();
        return out.toByteArray();
    }*/



}
